﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DotNetNuke.Common;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;

namespace BrandonHaynes.Providers
    {
    /// <summary>
    /// This class represents the simplest folder provider possible.  It
    /// does nothing except expose a "hello-world.txt" file on the 
    /// file system.  It is intended as a demonstration, and has nothing
    /// to do with the larger project.
    /// </summary>
    public class HelloWorldFolderProvider : FolderProvider
        {
        private const string Filename = "hello-world.txt";
        private const string HelloWorld = "Hello world!";

        public override bool FileExists(IFolderInfo folder, string fileName)
            { return fileName == Filename; }

        public override bool FolderExists(string folderPath, FolderMappingInfo folderMapping)
            { return true; }

        public override FileAttributes? GetFileAttributes(IFileInfo file)
            { return null; }

        public override string[] GetFiles(IFolderInfo folder)
            { return new[] { Filename }; }

        public override long GetFileSize(IFileInfo file)
            { return Encoding.Default.GetByteCount(HelloWorld); }

        public override Stream GetFileStream(IFileInfo file)
            { return new MemoryStream(Encoding.Default.GetBytes(HelloWorld)); }

        public override Stream GetFileStream(IFolderInfo folder, string fileName)
            { return new MemoryStream(Encoding.Default.GetBytes(HelloWorld)); }

        public override string GetFileUrl(IFileInfo file)
            { return Globals.LinkClick("FileID=" + file.FileId, 
                                       new PortalSettings(file.PortalId).HomeTabId, 
                                       default(int)); }

        public override string GetFolderProviderIconPath()
            { return "/Providers/HtmlEditorProviders/Telerik/images/Emoticons/regular_smile.gif"; }

        public override DateTime GetLastModificationTime(IFileInfo file)
            { return DateTime.MinValue; }

        public override bool IsInSync(IFileInfo file)
            { return true; }

        public override IEnumerable<string> GetSubFolders(string folderPath, FolderMappingInfo folderMapping)
            { return Enumerable.Empty<string>(); }

        public override void AddFolder(string folderPath, FolderMappingInfo folderMapping)
            { FolderManager.Instance.Synchronize(folderMapping.PortalID, folderPath, true, true); }

        public override void RenameFolder(IFolderInfo folder, string newFolderName) { }
        public override void DeleteFolder(IFolderInfo folder) { }
        public override bool RequiresNetworkConnectivity { get { return false; } }
        public override bool SupportsFileAttributes() { return false; }

        #region Unsupported Operations

        public override void AddFile(IFolderInfo folder, string fileName, Stream content)
            { throw new NotSupportedException(); }

        public override void DeleteFile(IFileInfo file)
            { throw new NotSupportedException(); }

        public override void RenameFile(IFileInfo file, string newFileName)
            { throw new NotSupportedException(); }

        public override void SetFileAttributes(IFileInfo file, FileAttributes fileAttributes)
            { throw new NotSupportedException(); }

        public override void UpdateFile(IFileInfo file, Stream content)
            { throw new NotSupportedException(); }

        public override void UpdateFile(IFolderInfo folder, string fileName, Stream content)
            { throw new NotSupportedException(); }

        #endregion
        }
    }